package top.healthylife.gateway.platform.youshu.api.api.aggregationapi;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.healthylife.gateway.core.service.ParentFace;
import top.healthylife.gateway.dto.annotation.ApiServiceExplain;
import top.healthylife.gateway.platform.youshu.api.api.YoushuEnterpriseCreditInfoApi;
import top.healthylife.gateway.platform.youshu.dto.YoushuResAggregationDataDto;
import top.healthylife.gateway.platform.youshu.dto.apia001.req.YoushuReqEnterpriseCreditInfoDto;
import top.healthylife.gateway.platform.youshu.dto.apia001.res.*;

import java.util.List;

/**
 * @author : GaoZeXi
 * @date 2022-2-17
 */
@Slf4j
@Service
@ApiServiceExplain(docUrl = "", price = 20, apiName = "数据聚合接口", info = "数据聚合接口,业务可直接调用,数据来源于[企业综合信用信息]接口")
public class YoushuAggregationDataApi implements ParentFace<YoushuReqEnterpriseCreditInfoDto, YoushuResAggregationDataDto> {

    @Autowired
    YoushuEnterpriseCreditInfoApi youshuEnterpriseCreditInfoApi;


    @Override
    public YoushuResAggregationDataDto execute(YoushuReqEnterpriseCreditInfoDto dto) throws Exception {
        YoushuResAggregationDataDto youshuResAggregationDataDto = new YoushuResAggregationDataDto();
        YoushuResEnterpriseCreditInfoDto execute = youshuEnterpriseCreditInfoApi.execute(dto);
        YoushuResEnterpriseCreditInfoItem data = execute.getData();
        //近90天法定代表人变更次数
        /*
         alterList（企业变更信息）中
         altDate（变更日期）在近90天，altItem='法定代表人变更'的
         altItem（变更事项）次数
         */
        List<EnterpriseChangeInfo> alterList = data.getAlterList();
        Integer countOver90Days = 0;
        Integer countOver360Days = 0;
        for (int i = 0; i < alterList.size(); i++) {
            EnterpriseChangeInfo enterpriseChangeInfo = alterList.get(i);
            DateTime parse = DateUtil.parse(enterpriseChangeInfo.getAltDate(), "yyyy-MM-dd");
            DateTime now = DateTime.now();
            //, BetweenFormatter.Level.DAY
            long between = now.between(parse.toJdkDate(), DateUnit.DAY);
            if (between <= 90) {
                countOver90Days++;
            } else if (between <= 360) {
                countOver360Days++;
            }
        }

        //近360天法定代表人变更次数
        /*
         alterList（企业变更信息）中
         altDate（变更日期）在近360天，altItem='法定代表人变更'的
         altItem（变更事项）次数
         */
        youshuResAggregationDataDto.setTimesOfLegalPersonChangeOverLast90Days(countOver90Days)
                .setTimesOfLegalPersonChangeOverLast360Days(countOver360Days);

        /*近360天列入严重违法次数
         illegalList（严重违法失信信息）中
         illegalDateIn在近360天，illegalDateOut（移出日期）为空的
         illegalResultIn（移入原因）次数
         */
        List<SeriouslyIllegalInfo> illegalList = data.getIllegalList();

        long count = illegalList.stream().filter(e -> {
            String illegalDateIn = e.getIllegalDateIn();
            String illegalDateOut = e.getIllegalDateOut();
            DateTime parse = DateUtil.parse(illegalDateIn, "yyyy-MM-dd");
            DateTime now = DateTime.now();
            //, BetweenFormatter.Level.DAY
            long between = now.between(parse.toJdkDate(), DateUnit.DAY);
            return between <= 360 && ObjectUtil.isEmpty(illegalDateOut);
        }).count();
        youshuResAggregationDataDto.setTimesOfBeMarkedSeriousViolationOverLast360Days(Math.toIntExact(count));
        //近180天列入税收违法次数
        /*
        caseInfoList（行政处罚信息）中
        penDecissDate在近180天，illegactTypeName（移出日期）=***的
        penResultName（行政处罚内容）次数
         */
        List<CaseInfoList> caseInfoList = data.getCaseInfoList();
        long countOver180Days = caseInfoList.stream().filter(e -> {
            String penDecissDate = e.getPenDecissDate();
            String illegactTypeName = e.getIllegactTypeName();
            DateTime parse = DateUtil.parse(penDecissDate, "yyyy-MM-dd");
            DateTime now = DateTime.now();
            long between = now.between(parse.toJdkDate(), DateUnit.DAY);
            return between <= 180 && "***".equals(illegactTypeName);
        }).count();
        youshuResAggregationDataDto.setTimesOfBeMarkedTaxViolationOverLast180Days(Math.toIntExact(countOver180Days));
        return youshuResAggregationDataDto;
    }
}
